home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / CLASSINC.PAK / MEMMGR.H < prev    next >
C/C++ Source or Header  |  1997-05-05  |  14KB  |  493 lines

  1. //----------------------------------------------------------------------------
  2. // Borland BIDS Container Library
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.11  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(CLASSLIB_MEMMGR_H)
  9. #define CLASSLIB_MEMMGR_H
  10.  
  11. #if !defined(CLASSLIB_DEFS_H)
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined(CLASSLIB_STDTEMPL_H)
  15. # include <classlib/stdtempl.h>
  16. #endif
  17. #if !defined(CLASSLIB_ALLOCTR_H)
  18. # include <classlib/alloctr.h>
  19. #endif
  20. #if !defined(SERVICES_CHECKS_H)
  21. # include <services/checks.h>
  22. #endif
  23.  
  24. #if !defined(__STDLIB_H) && !defined(_INC_STDLIB)
  25. # include <stdlib.h>
  26. #endif
  27. #if !defined(__DOS_H) && !defined(_INC_DOS)
  28. # include <dos.h>
  29. #endif
  30.  
  31. //#pragma option -Vo-
  32. #if defined( BI_CLASSLIB_NO_po )
  33. # pragma option -po-
  34. #endif
  35.  
  36. #if defined(BI_NAMESPACE)
  37. namespace ClassLib {
  38. #endif
  39.  
  40. /*------------------------------------------------------------------------*/
  41. /*                                                                        */
  42. /*  template <class Alloc> class TMBlockList                              */
  43. /*                                                                        */
  44. /*  Used internally.                                                      */
  45. /*                                                                        */
  46. /*------------------------------------------------------------------------*/
  47.  
  48. template <class Alloc> class TMBaseMemBlocks;
  49.  
  50. template <class Alloc> class TMBlockList
  51. {
  52.  
  53. public:
  54.  
  55.     TMBlockList( TMBlockList * );
  56.  
  57. private:
  58.  
  59.     void *operator new( size_t, size_t, const Alloc& );
  60.     void operator delete( void * );
  61.  
  62.     TMBlockList *Next;
  63.  
  64.     friend TMBaseMemBlocks<Alloc>;
  65.  
  66. };
  67.  
  68. template <class Alloc>
  69. inline TMBlockList<Alloc>::TMBlockList( TMBlockList<Alloc> *nxt ) :
  70.     Next( nxt )
  71. {
  72. }
  73.  
  74. template <class Alloc>
  75. inline void *TMBlockList<Alloc>::operator new ( size_t sz,
  76.                                             size_t extra,
  77.                                             const Alloc& a )
  78. {
  79.     return new(a)_TCHAR[sz+extra];
  80. }
  81.  
  82. template <class Alloc>
  83. inline void TMBlockList<Alloc>::operator delete ( void *ptr )
  84. {
  85.     Alloc::operator delete [] (ptr);
  86. }
  87.  
  88. #if defined( BI_OLDNAMES )
  89. #define MBlockList TMBlockList
  90. #endif
  91.  
  92. /*------------------------------------------------------------------------*/
  93. /*                                                                        */
  94. /*  class TBlockList                                                      */
  95. /*                                                                        */
  96. /*  Used internally.                                                      */
  97. /*                                                                        */
  98. /*------------------------------------------------------------------------*/
  99.  
  100. class TBlockList : public TMBlockList<TStandardAllocator>
  101. {
  102.  
  103. public:
  104.  
  105.     TBlockList( TBlockList *blk ) :
  106.         TMBlockList<TStandardAllocator>( blk ) {}
  107.  
  108. };
  109.  
  110. #if defined( BI_OLDNAMES )
  111. #define BlockList TBlockList
  112. #endif
  113.  
  114. /*------------------------------------------------------------------------*/
  115. /*                                                                        */
  116. /*  template <class Alloc> class TMBaseMemBlocks                          */
  117. /*                                                                        */
  118. /*  Used internally.                                                      */
  119. /*                                                                        */
  120. /*------------------------------------------------------------------------*/
  121.  
  122. template <class Alloc> class TMBaseMemBlocks :
  123.     public Alloc
  124. {
  125.  
  126. public:
  127.  
  128.     TMBaseMemBlocks( size_t = 8192 );
  129.     ~TMBaseMemBlocks();
  130.  
  131.     _TCHAR *Block() const { return REINTERPRET_CAST(_TCHAR *,CurBlock); }
  132.     unsigned Count() const { return BlockCount; }
  133.     int AllocBlock( size_t );
  134.     void FreeTo( unsigned );
  135.     size_t GetBlockSize() const { return BlockSize; }
  136.  
  137. private:
  138.  
  139.     TMBlockList<Alloc> *CurBlock;
  140.     const size_t BlockSize;
  141.     unsigned BlockCount;
  142.  
  143. };
  144.  
  145. template <class Alloc>
  146. inline TMBaseMemBlocks<Alloc>::TMBaseMemBlocks( size_t sz ) :
  147.     CurBlock(0),
  148.     BlockCount(0),
  149.     BlockSize(sz)
  150. {
  151.     CHECK( sz != 0 );
  152. }
  153.  
  154. template <class Alloc>
  155. inline TMBaseMemBlocks<Alloc>::~TMBaseMemBlocks()
  156. {
  157. #if !defined( BI_WINDOWS_WEP_BUG )
  158.     FreeTo( 0 );
  159. #endif
  160. }
  161.  
  162. template <class Alloc> int TMBaseMemBlocks<Alloc>::AllocBlock( size_t sz )
  163. {
  164.     TMBlockList<Alloc> *temp =
  165.         new( max(sz,BlockSize), *this ) TMBlockList<Alloc>( CurBlock );
  166.     CurBlock = temp+1;
  167.     BlockCount++;
  168.     return 1;
  169. }
  170.  
  171. template <class Alloc> void TMBaseMemBlocks<Alloc>::FreeTo( unsigned term )
  172. {
  173.     PRECONDITION( BlockCount >= term );
  174.     while( BlockCount > term )
  175.         {
  176.         TMBlockList<Alloc> *temp = CurBlock-1;
  177.         CurBlock = temp->Next;
  178.         delete[] temp;
  179.         BlockCount--;
  180.         }
  181. }
  182.  
  183. #if defined( BI_OLDNAMES )
  184. #define MBaseMemBlocks TMBaseMemBlocks
  185. #endif
  186.  
  187. /*------------------------------------------------------------------------*/
  188. /*                                                                        */
  189. /*  class TBaseMemBlocks                                                  */
  190. /*                                                                        */
  191. /*  Used internally.                                                      */
  192. /*                                                                        */
  193. /*------------------------------------------------------------------------*/
  194.  
  195. class TBaseMemBlocks : public TMBaseMemBlocks<TStandardAllocator>
  196. {
  197.  
  198. public:
  199.  
  200.     TBaseMemBlocks( size_t sz = 8192 ) :
  201.         TMBaseMemBlocks<TStandardAllocator>(sz) {}
  202.  
  203. };
  204.  
  205. #if defined( BI_OLDNAMES )
  206. #define BaseMemBlocks TBaseMemBlocks
  207. #endif
  208.  
  209. /*------------------------------------------------------------------------*/
  210. /*                                                                        */
  211. /*  template <class Alloc> class TMMemStack                               */
  212. /*                                                                        */
  213. /*  Managed memory stack.  Implements mark and release style memory       */
  214. /*  management, using the allocator Alloc.                                */
  215. /*                                                                        */
  216. /*------------------------------------------------------------------------*/
  217.  
  218. template <class Alloc> class TMMarker;
  219.  
  220. template <class Alloc> class TMMemStack
  221. {
  222.  
  223. public:
  224.  
  225.     friend TMMarker<Alloc>;
  226.  
  227.     TMMemStack( size_t = 8192 );
  228.  
  229.     void *Allocate( size_t );
  230.  
  231. #if defined( BI_OLDNAMES )
  232.     void *allocate( size_t sz ) { return Allocate( sz ); }
  233. #endif  // BI_OLDNAMES
  234.  
  235. private:
  236.  
  237.     TMBaseMemBlocks<Alloc> Data;
  238.     size_t CurLoc;
  239. };
  240.  
  241. #if defined(BI_OLDNAMES)
  242. # define MMemStack TMMemStack
  243. #endif
  244.  
  245. template <class Alloc>
  246. inline void *operator new( size_t sz, TMMemStack<Alloc>& m )
  247. {
  248.     return m.Allocate( sz );
  249. }
  250.  
  251. #if !defined(BI_NO_ARRAYNEW)
  252. template <class Alloc>
  253. inline void *operator new [] ( size_t sz, TMMemStack<Alloc>& m )
  254. {
  255.     return m.Allocate( sz );
  256. }
  257. #endif
  258.  
  259. template <class Alloc> inline TMMemStack<Alloc>::TMMemStack( size_t sz ) :
  260.     Data( sz ),
  261.     CurLoc(sz)
  262. {
  263.     CHECK( sz != 0 );
  264. }
  265.  
  266. template <class Alloc> void *TMMemStack<Alloc>::Allocate( size_t sz )
  267. {
  268.     sz = max( 1U, sz );
  269.     if( sz > Data.GetBlockSize() - CurLoc )
  270.         if( Data.AllocBlock( sz ) == 0 )
  271.             return 0;
  272.         else
  273.             CurLoc = 0;
  274.     void *temp = Data.Block() + CurLoc;
  275.     CurLoc += sz;
  276.     return temp;
  277. }
  278.  
  279. /*------------------------------------------------------------------------*/
  280. /*                                                                        */
  281. /*  template <class Alloc> class TMMarker                                 */
  282. /*                                                                        */
  283. /*  Provides the mark for TMMemStack.                                     */
  284. /*                                                                        */
  285. /*------------------------------------------------------------------------*/
  286.  
  287. template <class Alloc> class TMMarker
  288. {
  289.  
  290. public:
  291.  
  292.     TMMarker( TMMemStack<Alloc>& ms ) :
  293.         Memstk(ms),
  294.         Blk(ms.Data.Count()),
  295.         CurLoc(ms.CurLoc)
  296.         {
  297.         }
  298.  
  299.     ~TMMarker()
  300.         {
  301.         PRECONDITION( Blk < Memstk.Data.Count() ||
  302.               (Blk == Memstk.Data.Count() && CurLoc <= Memstk.CurLoc )
  303.             );
  304.         Memstk.Data.FreeTo( Blk );
  305.         Memstk.CurLoc = CurLoc;
  306.         }
  307.  
  308.  
  309. private:
  310.  
  311.     TMMemStack<Alloc>& Memstk;
  312.     const unsigned Blk;
  313.     const size_t CurLoc;
  314.  
  315. };
  316.  
  317. #if defined( BI_OLDNAMES )
  318. #define MMarker TMMarker
  319. #endif
  320.  
  321. /*------------------------------------------------------------------------*/
  322. /*                                                                        */
  323. /*  class TMemStack                                                       */
  324. /*                                                                        */
  325. /*  Implements mark and release style memory management using the         */
  326. /*  standard allocator.                                                   */
  327. /*                                                                        */
  328. /*------------------------------------------------------------------------*/
  329.  
  330. class TMemStack : public TMMemStack<TStandardAllocator>
  331. {
  332. public:
  333.  
  334.     TMemStack( size_t sz = 8192 ) : TMMemStack<TStandardAllocator>(sz) {}
  335.  
  336. };
  337.  
  338. #if defined( BI_OLDNAMES )
  339. #define MemStack TMemStack
  340. #endif
  341.  
  342. /*------------------------------------------------------------------------*/
  343. /*                                                                        */
  344. /*  template <class Alloc> class TMarker                                  */
  345. /*                                                                        */
  346. /*  Provides the mark for TMemStack.                                      */
  347. /*                                                                        */
  348. /*------------------------------------------------------------------------*/
  349.  
  350. class TMarker : public TMMarker<TStandardAllocator>
  351. {
  352.  
  353. public:
  354.  
  355.     TMarker( TMMemStack<TStandardAllocator>& ms ) :
  356.         TMMarker<TStandardAllocator>(ms) {}
  357. };
  358.  
  359.  
  360. /*------------------------------------------------------------------------*/
  361. /*                                                                        */
  362. /*  template <class Alloc> class TMMemBlocks                              */
  363. /*                                                                        */
  364. /*  Managed single-size block allocator.  Allocates blocks                */
  365. /*  of the size specified in the constructor, using the memory            */
  366. /*  manager specified by Alloc.                                           */
  367. /*                                                                        */
  368. /*------------------------------------------------------------------------*/
  369.  
  370. template <class Alloc> class TMMemBlocks
  371. {
  372.  
  373. public:
  374.  
  375.     TMMemBlocks( size_t sz, unsigned count = 100 ) :
  376.       Mem( sz*count ),
  377.       FreeList(0),
  378.       Size( max(sz,sizeof(void *)) )
  379.     {
  380.       CHECK( sz != 0 && count != 0 );
  381.     }
  382.  
  383.     void *Allocate( size_t sz)
  384.     {
  385.       PRECONDITION( Size == max(sz,sizeof(void *)) );
  386.       if( FreeList == 0 )
  387.         return Mem.Allocate( Size );
  388.       else
  389.         {
  390.         void *temp = FreeList;
  391.         FreeList = *(void **)temp;
  392.         return temp;
  393.         }
  394.     }
  395.     void Free( void * block)
  396.     {
  397.       *(void **)block = FreeList;
  398.       FreeList = block;
  399.     }
  400.  
  401. #if defined( BI_OLDNAMES )
  402.     void *allocate( size_t sz ) { return Allocate(sz); }
  403.     void free( void *ptr ) { Free(ptr); }
  404. #endif  // BI_OLDNAMES
  405.  
  406. private:
  407.  
  408.     void *FreeList;
  409.     TMMemStack<Alloc> Mem;
  410.     size_t Size;
  411.  
  412. };
  413.  
  414. #if defined( BI_OLDNAMES )
  415. #define MMemBlocks TMemBlocks
  416. #endif
  417.  
  418. #if defined(FOO)    
  419. template <class Alloc>
  420. inline TMMemBlocks<Alloc>::TMMemBlocks( size_t sz, unsigned count ) :
  421.     Mem( sz*count ),
  422.     FreeList(0),
  423.     Size( max(sz,sizeof(void *)) )
  424. {
  425.     CHECK( sz != 0 && count != 0 );
  426. }
  427.  
  428. #if (__DEBUG > 0)
  429. template <class Alloc> inline void *TMMemBlocks<Alloc>::Allocate( size_t sz )
  430. #else
  431. template <class Alloc> inline void *TMMemBlocks<Alloc>::Allocate( size_t )
  432. #endif
  433. {
  434.     PRECONDITION( Size == max(sz,sizeof(void *)) );
  435.     if( FreeList == 0 )
  436.         return Mem.Allocate( Size );
  437.     else
  438.         {
  439.         void *temp = FreeList;
  440.         FreeList = *(void **)temp;
  441.         return temp;
  442.         }
  443. }
  444.  
  445. template <class Alloc>
  446. inline void TMMemBlocks<Alloc>::Free( void *block )
  447. {
  448.     *(void **)block = FreeList;
  449.     FreeList = block;
  450. }
  451. #endif
  452.  
  453. /*------------------------------------------------------------------------*/
  454. /*                                                                        */
  455. /*  class TMemBlocks                                                      */
  456. /*                                                                        */
  457. /*  Single-size block allocator.  Allocates blocks of the size            */
  458. /*  specified in the constructor, using the global operator new           */
  459. /*  and operator delete.                                                  */
  460. /*                                                                        */
  461. /*------------------------------------------------------------------------*/
  462.  
  463. class TMemBlocks : public TMMemBlocks<TStandardAllocator>
  464. {
  465.  
  466. public:
  467.  
  468.     TMemBlocks( size_t sz, unsigned n = 100 ) :
  469.         TMMemBlocks<TStandardAllocator>( sz, n ) {}
  470.  
  471. };
  472.  
  473. #if defined(BI_OLDNAMES)
  474. # define MemBlocks TMemBlocks
  475. #endif
  476.  
  477. typedef TMMemBlocks<TStandardAllocator> TStandardBlocks;
  478.  
  479. #if defined(BI_PLAT_WIN16) && !defined(BI_DATA_NEAR)
  480.  typedef TMMemBlocks<TSharedAllocator> TSharedBlocks;
  481. #endif
  482.  
  483. #if defined(BI_NAMESPACE)
  484. }   // namespace ClassLib
  485. #endif
  486.  
  487. #if defined( BI_CLASSLIB_NO_po )
  488. # pragma option -po.
  489. #endif
  490. //#pragma option -Vo.
  491.  
  492. #endif  // CLASSLIB_MEMMGR_H
  493.